﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class Player2Controller : MonoBehaviour
{
    public HPshow hpshow;
    GameBoard gameBoard;

    public float speed = 4f;
    public float speedFactor = 1f;
    public int energy = 0;
    public int asset = 0;

    private float shootGap = 1.0f;
    private float bombGap = 1.0f;
    private float laserGap = 1.0f;
    private float buildBlockGap = 0.5f;
    private float buildWaterGap = 0.5f;
    private float buildTreeGap = 0.5f;
    private float assetGap = 1f;

    private Rigidbody2D rb2d;
    public Transform shootArea;
    public Transform laser;
    public Transform playerPos;

    Animator animator;
    PlayerInput playerInput;

    private bool canShoot = true;
    private bool canBomb = true;
    private bool canLaser = true;
    private bool canBuildBlock = true;
    private bool canBuildWater = true;
    private bool canBuildTree = true;
    private int buildType = 0;

    private int hp = 100;
    public FillController[] fills;

    bool isinvicible=false;
    float invicibletimer=1.0f;
    private void GetHurt(int hurt) {
        if (isinvicible) return;
        hp -= hurt;
        isinvicible = true;
    }

    private void GetEnergy() {
        energy += 100;
        energy = Mathf.Min(energy, 500);
    }
    SpriteRenderer sprite;
    private void Awake()
    {
        sprite = GetComponent<SpriteRenderer>();
        gameBoard = GameObject.Find("Board").GetComponent<GameBoard>();

        fills=GameObject.Find("Canvas").GetComponentsInChildren<FillController>();
        animator = gameObject.GetComponent<Animator>();
        rb2d = gameObject.GetComponent<Rigidbody2D>();
        shootArea = transform.Find("ShootArea");
        laser = transform.Find("Laser");
        playerInput = gameObject.GetComponent<PlayerInput>();
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    void updateGap() {
        float time = Time.deltaTime;
        if (shootGap > 0) {
            shootGap -= time;
        }
        if (bombGap > 0) {
            bombGap -= time;
        }
        if (laserGap > 0) {
            laserGap -= time;
        }
        if (buildBlockGap > 0) {
            buildBlockGap -= time;
        }
        if (buildWaterGap > 0) {
            buildWaterGap -= time;
        }
        if (buildTreeGap > 0) {
            buildTreeGap -= time;
        }
        if (assetGap > 0) {
            assetGap -= time;
        }
    }

    LoadSceneParameters parameter;
    // Update is called once per frame
    void Update()
    {
        if (isinvicible)
        {
            invicibletimer -= Time.deltaTime;
            if (invicibletimer <= 0)
            {
                invicibletimer = 1.0f;
                isinvicible = false;
            }
            else
            {
                float remainder = invicibletimer % 0.1f;
                sprite.enabled = remainder > 0.05f;
            }
        }
        else
            sprite.enabled = true;

        float moveX = playerInput.moveX;
        float moveY = playerInput.moveY;
        if (moveX != 0 || moveY != 0)
        {
            animator.SetFloat("X", moveX);
            animator.SetFloat("Y", moveY);
        }

        hpshow.hp = hp;
        animator.SetFloat("Mag", playerInput.mag);

        updateGap();

        if (playerInput.shoot && shootGap <=0 && canShoot)
        {
            fills[2].gotit = true;
            animator.SetTrigger("Shoot");
            shootGap = 1f;            
        }
        if (playerInput.bomb && bombGap <= 0 && canBomb && energy>10)
        {
            fills[0].gotit = true;
            energy -= 10;
            animator.SetTrigger("Bomb");
            bombGap = 1f;            
        }
        if (playerInput.laser && laserGap <=0 && canLaser && energy>50) {
            fills[1].gotit = true;
            energy -= 50;
            animator.SetTrigger("Laser");
            laserGap = 1f;            
        }
        if (playerInput.buildBlock && buildBlockGap <=0 && canBuildBlock && asset>10) {
            fills[5].gotit = true;
            animator.SetTrigger("Build");
            asset -= 10;
            buildBlockGap = 1f;
            buildType = 0;
        }        
        if (playerInput.buildWater && buildWaterGap <=0 && canBuildWater && asset>50)
        {
            fills[4].gotit = true;
            animator.SetTrigger("Build");
            asset -= 50;
            buildWaterGap = 1f;
            buildType = 1;                            
        }
        if (playerInput.buildTree && buildTreeGap <= 0 && canBuildTree && asset>20) {
            fills[3].gotit = true;
            animator.SetTrigger("Build");
            asset -= 20;
            buildTreeGap = 1;
            buildType = 2;            
        }
        if (playerInput.mode == 1) {
            if (assetGap <= 0 && asset < 500) {
                asset += 10;
                assetGap = 1f;
            }
        }
        if (hp <= 0)
        {
            SceneManager.LoadScene("win");
        }
        UpdateUI();
    }

    private void FixedUpdate()
    {

        //Vector2 moveVec = SquareToCircle(playerInput.moveX, playerInput.moveY);
        rb2d.transform.position += speed * speedFactor * Time.fixedDeltaTime * playerInput.moveVec;
        playerPos.localPosition = rb2d.transform.localPosition;

        if (animator.GetFloat("X") > 0)
        {
            if (shootArea.localScale.x > 0)
            {
                shootArea.localScale = new Vector3(-1f, 1f, 1f);
            }
        }
        else
        {
            if (shootArea.localScale.x < 0)
            {
                shootArea.localScale = new Vector3(1f, 1f, 1f);
            }
        }
        
        
    }

    Vector2 SquareToCircle(float x, float y)
    {
        Vector2 output = Vector2.zero;
        output.x = x * Mathf.Sqrt(1 - (y * y) / 2);
        output.y = y * Mathf.Sqrt(1 - (x * x) / 2);
        return output;
    }
   

    private void OnShootEnter() {
        playerInput.isEnabled = false;
        lockBtn();
    }

    private void OnShootExit() {
        playerInput.isEnabled = true;
        unLockBtn();
        shootArea.GetComponent<ShootAreaSensor>().HurtTarget(50);
    }

    private void OnBombEnter() {
        playerInput.isEnabled = false;
        lockBtn();
    }

    private void OnBombExit() {
        playerInput.isEnabled = true;
        unLockBtn();
        gameBoard.buildBomb(transform);
    }

    private void OnBuildEnter() {
        playerInput.isEnabled = false;
        lockBtn();
    }

    private void OnBuildExit() {
        playerInput.isEnabled = true;        
        unLockBtn();
        gameBoard.buildBlock(transform.position, buildType);
    }

    private void OnLaserEnter() {
        Debug.Log("laser");
        playerInput.isEnabled = false;
        laser.gameObject.GetComponent<LaserController>().enabled = true;
        lockBtn();
    }

    private void OnLaserExit() {
        playerInput.isEnabled = true;
        laser.gameObject.GetComponent<LaserController>().hurtTargetTable();
        //laser.gameObject.GetComponent<LaserController>().enabled = false;
        //laser.localScale = new Vector3(0, 0, 0);
        unLockBtn();
    }

    private void lockBtn() {
        canShoot = false;
        canBomb = false;
        canLaser = false;
        canBuildBlock = false;
        canBuildWater = false;
        canBuildTree = false;
    }

    private void unLockBtn()
    {
        canShoot = true;
        canBomb = true;
        canLaser = true;
        canBuildBlock = true;
        canBuildWater = true;
        canBuildTree = true;
    }

    private void EnterTree(bool burn) {        
        speedFactor = 0.8f;
    }

    private void ExitTree() {        
        speedFactor = 1.0f;
    }

    private void EnterWater()
    {
        speedFactor = 0.6f;
    }

    private void ExitWater() {
        speedFactor = 1f;
    }

    private void EnterSlime() {
        speedFactor = 0.4f;
    }

    private void ExitSlime() {
        speedFactor = 1.0f;
    }

    VerticalFill ver;
    private void UpdateUI()
    {
        if (playerInput.mode == 1)
        {
            ver = GameObject.Find("resource").GetComponentInChildren<VerticalFill>();
            ver.Filling(asset / 500f);
        }
        else {
            ver = GameObject.Find("energy").GetComponentInChildren<VerticalFill>();
            ver.Filling(energy / 500f);
        }
    }
}
